<!DOCTYPE html>
<html>
<head>
    <title>插件管理</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <!-- 指定以IE8的方式来渲染 -->
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"/>
    <link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <link type="text/css" rel="stylesheet" href="/css/jsherp.css"/>
    <link rel="stylesheet" type="text/css" href="/js/easyui/themes/gray/easyui.css"/>
    <link rel="stylesheet" type="text/css" href="/js/easyui/themes/icon.css"/>
    <link type="text/css" rel="stylesheet" href="/css/common.css"/>
    <link rel="stylesheet" type="text/css" href="/js/Huploadify/Huploadify.css"/>
    <script type="text/javascript" src="/js/jquery-1.8.0.min.js"></script>
    <script type="text/javascript" src="/js/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/js/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/js/common/common.js"></script>
    <script src="/js/Huploadify/jquery.Huploadify.js"></script>
</head>
<body>
<!-- 数据显示table -->
<div id="tablePanel" class="easyui-panel" data-options="fit:true" style="padding:5px;" title="插件管理" iconCls="icon-list">
    <div class="box-body form-inline">
        <div class="form-group">
            <label class="control-label">名称：</label>
            <div class="control-inline">
                <input type="text" id="searchPluginName" name="searchPluginName" value="" maxlength="100" class="easyui-textbox width-90"/>
            </div>
        </div>
        <div class="form-group">
            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-search" id="searchBtn">查询</a>&nbsp;
            <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-redo" id="searchResetBtn">重置</a>
        </div>
        <div class="form-group">
            <a href="javascript:void(0)" class="easyui-linkbutton" id="submitActivationCode">填写激活码</a>
        </div>
    </div>
    <table id="tableData" style="height:340px;top:300px;border-bottom-color:#FFFFFF"></table>
</div>
<div id="pluginDlg" class="easyui-dialog" style="width:650px;padding:10px 20px"
     closed="true" buttons="#dlg-buttons" modal="true" cache="false" collapsible="false" closable="true">
    <form id="pluginFM" method="post" novalidate>
        <table>
            <tr>
                <td style="width:60px;">插件包</td>
                <td style="padding:5px">
                    <div id="pluginFile"></div>
                </td>
            </tr>
            <tr>
                <td style="width:60px;"></td>
                <td style="padding:5px">
                    (扩展名必须为jar)
                </td>
            </tr>
        </table>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" id="cancelPlugin" class="easyui-linkbutton" iconCls="icon-cancel"
       onclick="javascript:$('#pluginDlg').dialog('close')">取消</a>
</div>

<div id="activationCodeDlg" class="easyui-dialog" style="width:420px;padding:10px 20px"
     closed="true" buttons="#activationCodeDlg-buttons" modal="true" cache="false" collapsible="false" closable="true">
    <form id="activationCodeFM" method="post" novalidate>
        <table>
            <tr>
                <td>机器码&nbsp;&nbsp;</td>
                <td style="padding:5px">
                    <span id="machineCode"></span>
                </td>
            </tr>
            <tr>
                <td>激活码&nbsp;&nbsp;</td>
                <td style="padding:5px">
                    <input name="activationCode" id="activationCode" class="easyui-textbox" data-options="multiline:true" style="width: 280px;height: 60px;"/>
                </td>
            </tr>
        </table>
    </form>
</div>
<div id="activationCodeDlg-buttons">
    <a href="javascript:void(0)" id="saveActivationCode" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
    <a href="javascript:void(0)" id="cancelActivationCode" class="easyui-linkbutton" iconCls="icon-cancel"
       onclick="javascript:$('#activationCodeDlg').dialog('close')">取消</a>
</div>

<script type="text/javascript">
    //初始化界面
    $(function () {
        initTableData();
        ininPager();
    });

    //初始化表格数据
    function initTableData() {
        $('#tableData').datagrid({
            height: heightInfo,
            nowrap: false,
            rownumbers: true,
            //动画效果
            animate: false,
            //选中单行
            singleSelect: true,
            collapsible: false,
            selectOnCheck: false,
            //单击行是否选中
            checkOnSelect: false,
            //交替出现背景
            striped: true,
            pagination: true,
            pageSize: initPageSize,
            pageList: initPageNum,
            columns: [[
                {field: 'id', width: 35, align: "center", checkbox: true},
                {
                    title: '操作', field: 'op', align: "center", width: 120,
                    formatter: function (value, rec, index) {
                        var str = '';
                        str += '<img title="上传页面" src="/js/easyui/themes/icons/edit_add.png" style="cursor: pointer;" onclick="uploadTemplate(\'' + rec.path + '\');"/>&nbsp;&nbsp;&nbsp;';
                        str += '<img title="开启" src="/js/easyui/themes/icons/ok.png" style="cursor: pointer;" onclick="startPlugin(\'' + rec.pluginDescriptor.pluginId + '\');"/>&nbsp;&nbsp;&nbsp;';
                        str += '<img title="停止" src="/js/easyui/themes/icons/no.png" style="cursor: pointer;" onclick="stopPlugin(\'' + rec.pluginDescriptor.pluginId + '\');"/>&nbsp;&nbsp;&nbsp;';
                        str += '<img title="卸载" src="/js/easyui/themes/icons/undo.png" style="cursor: pointer;" onclick="uninstallPlugin(\'' + rec.pluginDescriptor.pluginId + '\');"/>';
                        return str;
                    }
                },
                {title: '标识', field: 'pluginId', width: 180,
                    formatter: function(value, rec) {
                        if (rec) {
                            return rec.pluginDescriptor.pluginId;
                        }
                    }
                },
                {title: '名称', field: 'pluginName', width: 120,
                    formatter: function(value, rec) {
                        if (rec) {
                            var desc = rec.pluginDescriptor.pluginDescription;
                            if(desc.indexOf("|")){
                                var arr = desc.split("|");
                                return arr[0];
                            }
                        }
                    }
                },
                {title: '版本', field: 'pluginVersion', width: 120,
                    formatter: function(value, rec) {
                        if (rec) {
                            return rec.pluginDescriptor.version;
                        }
                    }
                },
                {title: '作者', field: 'provider', width: 100,
                    formatter: function(value, rec) {
                        if (rec) {
                            return rec.pluginDescriptor.provider;
                        }
                    }
                },
                {title: '页面链接', field: 'pluginUrl', width: 250,
                    formatter: function(value, rec) {
                        if (rec) {
                            var desc = rec.pluginDescriptor.pluginDescription;
                            if(desc.indexOf("|")){
                                var arr = desc.split("|");
                                return "<a href='" + arr[1] + "' target='_blank'>" + arr[1] + "</a>";
                            }
                        }
                    }
                },
                {title: '状态', field: 'pluginState', width: 100,
                    formatter: function(value, rec) {
                        if (rec) {
                            if(rec.pluginState == "STARTED") {
                                return "<span style='color:green'>开启</span>";
                            } else if(rec.pluginState == "STOPPED") {
                                return "<span style='color:red'>停止</span>";
                            }
                        }
                    }
                }
            ]],
            toolbar: [
                {
                    id: 'addPlugin',
                    text: '上传插件包',
                    iconCls: 'icon-add',
                    handler: function () {
                        addPlugin();
                    }
                }
            ],
            onLoadError: function () {
                $.messager.alert('页面加载提示', '页面加载异常，请稍后再试！', 'error');
                return;
            }
        });
        dgResize();
        showPluginDetails(1, initPageSize);
    }

    //分页信息处理
    function ininPager() {
        try {
            var opts = $("#tableData").datagrid('options');
            var pager = $("#tableData").datagrid('getPager');
            pager.pagination({
                onSelectPage: function (pageNum, pageSize) {
                    opts.pageNumber = pageNum;
                    opts.pageSize = pageSize;
                    pager.pagination('refresh', {
                        pageNumber: pageNum,
                        pageSize: pageSize
                    });
                    showPluginDetails(pageNum, pageSize);
                }
            });
        }
        catch (e) {
            $.messager.alert('异常处理提示', "分页信息异常 :  " + e.name + ": " + e.message, 'error');
        }
    }

    //停止插件
    function stopPlugin(pluginId) {
        $.messager.confirm('停止确认', '确定要停止该插件吗？', function (r) {
            if (r) {
                $.ajax({
                    type: "post",
                    url: "/plugin/stop/" + pluginId,
                    dataType: "json",
                    success: function (res) {
                        if(res && res.code == 200) {
                            $("#searchBtn").click();
                        } else {
                            $.messager.alert('停止提示', '停止插件失败，请稍后再试！', 'error');
                        }
                    },
                    //此处添加错误处理
                    error: function () {
                        $.messager.alert('停止提示', '停止插件异常，请稍后再试！', 'error');
                        return;
                    }
                });
            }
        });
    }

    //卸载插件
    function uninstallPlugin(pluginId) {
        $.messager.confirm('卸载确认', '确定要卸载该插件吗？', function (r) {
            if (r) {
                $.ajax({
                    type: "post",
                    url: "/plugin/uninstall/" + pluginId,
                    dataType: "json",
                    success: function (res) {
                        if(res && res.code == 200) {
                            $("#searchBtn").click();
                        } else {
                            $.messager.alert('停止提示', '卸载插件失败，请稍后再试！', 'error');
                        }
                    },
                    //此处添加错误处理
                    error: function () {
                        $.messager.alert('停止提示', '卸载插件异常，请稍后再试！', 'error');
                        return;
                    }
                });
            }
        });
    }

    //增加
    var url;
    var pluginId = 0;
    //保存编辑前的名称
    var oldPluginNumber = "";

    function addPlugin() {
        $('#pluginDlg').dialog('open').dialog('setTitle', '<img src="/js/easyui/themes/icons/edit_add.png"/>&nbsp;上传插件包');
        $(".window-mask").css({width: webW, height: webH});
        $('#pluginFM').form('clear');
        fileUpload();
    }

    //文件上传
    function fileUpload() {
        $('#pluginFile').empty().Huploadify({
            auto:true,
            fileTypeExts:'*.jar',
            maxFileNumber:1,
            fileSizeLimit:10240,
            showUploadedPercent:true,//是否实时显示上传的百分比，如20%
            showUploadedSize:true,
            removeTimeout:9999999,
            uploader:'/plugin/uploadInstallPluginJar',
            onUploadStart:function(){
                //alert('开始上传');
            },
            onInit:function(){
                //alert('初始化');
            },
            onUploadComplete:function(){
                $("#searchBtn").click();
                alert('上传完成');
            },
            onDelete:function(file){
                console.log('删除的文件：'+file);
                console.log(file);
            }
        });
    }

    //上传页面
    function uploadTemplate(path) {
        var rootPath = path.substring(0, path.indexOf("plugins"));
        $.messager.alert('上传页面', '请将页面上传到服务器目录：<br/>' + rootPath + "erp_web/plugins/", 'info');
    }

    //开启插件
    function startPlugin(pluginId) {
        $.messager.confirm('开启确认', '确定要开启该插件吗？', function (r) {
            if (r) {
                $.ajax({
                    type: "post",
                    url: "/plugin/start/" + pluginId,
                    dataType: "json",
                    success: function (res) {
                        if(res && res.code == 200) {
                            $("#searchBtn").click();
                        } else {
                            $.messager.alert('开启提示', '开启插件失败，请稍后再试！', 'error');
                        }
                    },
                    //此处添加错误处理
                    error: function () {
                        $.messager.alert('开启提示', '开启插件异常，请稍后再试！', 'error');
                        return;
                    }
                });
            }
        });
    }

    //搜索处理
    $("#searchBtn").unbind().bind({
        click: function () {
            showPluginDetails(1, initPageSize);
            var opts = $("#tableData").datagrid('options');
            var pager = $("#tableData").datagrid('getPager');
            opts.pageNumber = 1;
            opts.pageSize = initPageSize;
            pager.pagination('refresh', {
                pageNumber: 1,
                pageSize: initPageSize
            });
        }
    });

    function showPluginDetails(pageNo, pageSize) {
        $.ajax({
            type: "get",
            url: "/plugin/list",
            dataType: "json",
            data: ({
                name: $("#searchPluginName").textbox("getValue"),
                currentPage: pageNo,
                pageSize: pageSize
            }),
            success: function (res) {
                if(res && res.code === 200){
                    $("#tableData").datagrid('loadData', res.data);
                }
            },
            //此处添加错误处理
            error: function () {
                $.messager.alert('查询提示', '查询数据后台异常，请稍后再试！', 'error');
                return;
            }
        });
    }

    //重置按钮
    $("#searchResetBtn").unbind().bind({
        click: function () {
            $("#searchPluginName").textbox("setValue","");
            //加载完以后重新初始化
            $("#searchBtn").click();
        }
    });

    //填写激活码
    $("#submitActivationCode").unbind().bind({
        click: function () {
            $.ajax({
                type: "get",
                url: "/plugin/getMacWithSecret",
                dataType: "json",
                success: function (res) {
                    if(res && res.code == 200) {
                        $("#machineCode").text(res.data);
                        $('#activationCodeDlg').dialog('open').dialog('setTitle', '<img src="/js/easyui/themes/icons/edit_add.png"/>&nbsp;填写激活码');
                        $(".window-mask").css({width: webW, height: webH});
                        $("#activationCode").textbox("clear");
                        $.ajax({
                            type: "get",
                            url: "/platformConfig/getPlatformConfigByKey",
                            data: {
                                platformKey: "activation_code"
                            },
                            dataType: "json",
                            success: function (res) {
                                if(res && res.code == 200){
                                    $("#activationCode").textbox("setValue",res.data.platformValue);
                                }
                            },
                            //此处添加错误处理
                            error: function () {
                                $.messager.alert('提示', '保存信息异常，请稍后再试！', 'error');
                                return;
                            }
                        });
                    }
                },
                //此处添加错误处理
                error: function () {
                    $.messager.alert('查询提示', '查询数据后台异常，请稍后再试！', 'error');
                    return;
                }
            });
        }
    });

    $("#saveActivationCode").off("click").on("click",function () {
        if($("#activationCode").val()==""){
            $.messager.alert('提示', '激活码不能为空！', 'warning');
            return;
        }
        $.ajax({
            type: "post",
            url: "/platformConfig/updatePlatformConfigByKey",
            data: {
                platformKey: "activation_code",
                platformValue: $("#activationCode").val()
            },
            dataType: "json",
            success: function (res) {
                if(res && res.code == 200){
                    $.messager.alert('提示', '保存成功！', 'info');
                    $('#activationCodeDlg').dialog('close');
                }
            },
            //此处添加错误处理
            error: function () {
                $.messager.alert('提示', '保存信息异常，请稍后再试！', 'error');
                return;
            }
        });
    });
</script>
</body>
</html>